<template>
    <list-layout>
        <div slot="search">
            <el-form :model="queryParams" :inline="true" ref="queryForm" size="small" v-show="showSearch" label-width="88px">
                <el-row>
                    <el-col :span="6">
                        <!--            项目名称-->
                        <el-form-item :label="$t('bommaterial.projectName')" prop="bomProjectName">
                            <el-input v-model="queryParams.bomProjectName" :placeholder="$t('public.placeholder',{placeholder: $t('bommaterial.projectName')})" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <!--              厂区-->
                        <el-form-item :label="$t('mtool.factoryName')" prop="factoryName">
                            <el-select class="line_select" v-model="queryParams.factoryName" :placeholder="$t('formErrMsg.selectFactory')" filterable>
                                <el-option v-for="dict in factoryOptions" :key="dict.plantName" :label="dict.plantName" :value="dict.plantName" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6" align="left">
                        <el-form-item class="right-btns">
                            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('public.query')}}</el-button>
                            <el-button icon="el-icon-refresh" class="resectBtn" size="mini" @click="resetQuery">{{$t('public.reset')}}</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

        </div>
        <div class="app-container">
            <div class="container-header">
                <div class="header-left">
                    {{$t('defectIncoming.projectDetail')}}
                </div>
            </div>
            <el-row :gutter="10" class="table-action-wrap btnGroup">
                <el-col :span="1.5">
                    <el-button class="importitemBtn" size="mini" @click="handleImport"><img src="@/assets/images/icons/daoru.png" alt=""> {{$t('public.import')}}</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button class="exportitemBtn" size="mini" @click="handleExport">
                        <img src="@/assets/images/icons/daochu.png" alt="">
                        {{$t('public.export')}}
                    </el-button>
                </el-col>
            </el-row>

            <div class="container-table">
                <el-table stripe v-loading="loading" max-height="350" border :data="typeList" @selection-change="handleSelectionChange">

                    <!--          序号-->
                    <el-table-column :label="$t('public.serialNum')" type="index" width="55" align="center" />
                    <!--          项目名称-->
                    <el-table-column :label="$t('bommaterial.projectName')" align="left" minWidth="160" show-overflow-tooltip>
                        <template slot-scope="scope">
                            <div @click="linkBomMasterial(scope.row)" class="txtline">{{scope.row.bomProjectName}}</div>
                        </template>
                    </el-table-column>
                    <!--          版本号-->
                    <el-table-column :label="$t('inspectionManagement.version')" align="left" prop="version" minWidth="160" show-overflow-tooltip />
                    <!--          厂区-->
                    <el-table-column :label="$t('inspectionManagement.plantArea')" align="left" prop="factoryName" minWidth="160" show-overflow-tooltip />
                    <!--          来料总批次-->
                    <el-table-column :label="$t('defectIncoming.incomingTotalBatch')" align="left" prop="countLot" minWidth="160" show-overflow-tooltip />
                    <!--          合格批次数-->
                    <el-table-column :label="$t('defectIncoming.qualifiedTotalBatch')" align="left" prop="qualifiedLot" minWidth="160" show-overflow-tooltip />
                    <!--          不合格批次数-->
                    <el-table-column :label="$t('defectIncoming.unqualifiedTotalBatch')" align="left" prop="noQualifiedLot" minWidth="160" show-overflow-tooltip />
                    <!--          合格率-->
                    <el-table-column :label="$t('defectIncoming.qualifiedRate')" align="left" prop="passRate" minWidth="160" show-overflow-tooltip />

                </el-table>

                <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />
            </div>
            <!-- 导入对话框 -->
            <import-dialog :visible.sync="fileImportVisible" @callback="uploadCallback" :action="'/ny-iqc/bomAnalyse/import'" :templateUrl="'/static/analysis.xlsx'" fileName="BOM项目名称+厂区+版本号" />
            <!-- 新增|编辑｜查看 弹框 -->
            <!--      <mtool-detail :visible.sync="visible" v-model="form" :type="dialogType" @refresh="getList()"></mtool-detail>-->

        </div>
    </list-layout>
</template>
<script>
import {refreshCache} from '@/api/basic/dict/type'
import {listBomAnalyse} from '@/api/basic/bomAnalyse'
import {listALLFactory} from '@/api/basic/factory'
import {importAnalyse} from '@/api/basic/analysiss'
import importDialog from '../../components/importDialog'
// import MtoolDetail from './detial.vue'
//基础数据-库位信息管理
export default {
    name: 'location',
    dicts: [
        'NYIQC_GAUGE_TYPE', //量具类型
    ],
    components: { importDialog },
    data() {
        return {
            dialogType: 1, //1:新增  2：编辑 3：查看
            dialogForm: {},
            locationOptions: [],
            direction: 'rtl',
            addInspection: false,
            visible: false,
            drawer: false,
            approvalOpen: false,
            viewModel: {
                inspectionInformation: '0',
            },
            editModel: {
                inspectionInformation: '0',
            },
            approvalForm: {
                status: '1',
                textarea: '',
            },
            selBtn: 1,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 字典表格数据
            typeList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                factoryName: '',
                bomProjectName: '',
            },
            // 表单参数
            form: undefined,
            exportLoading: false,
            fullscreen: false,
            factoryOptions: [],
            fileImportVisible: false,
        }
    },
    created() {
        this.getList()
        this.factoryRemoteMethod()
    },
    methods: {
        drawerClose() {
            this.drawer = false
        },
        /** 查询字典类型列表 */
        getList() {
            this.loading = true
            listBomAnalyse(this.queryParams).then((response) => {
                this.typeList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                dictId: undefined,
                dictName: undefined,
                dictType: undefined,
                status: '0',
                remark: undefined,
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.form = {}
            this.dialogType = 1
            this.visible = true
        },
        handleApproval() {
            if (this.ids.length === 0) {
                this.$modal.msgWarning(this.$t('formErrMsg.selectOneInspect'))
                return
            }
            this.handleDelete()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
        },
        uploadCallback() {
            this.getList()
        },
        locationRemoteMethod(e) {
            listALLLocation({ locationName: e }).then((res) => {
                this.locationOptions = res.data
            })
        },
        handleLocationChange(e) {
            this.queryParams.locationName = e.join(',')
        },
        handleGaugeTypeChange(e) {
            this.queryParams.gaugeType = e.join(',')
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const dictIds = (row && row.id) || this.ids
            this.$modal
                .confirm('是否确认删除id为"' + dictIds + '"的数据项？')
                .then(function () {
                    return delGauge(dictIds)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess(this.$t('public.delSuc'))
                })
                .catch(() => {})
        },
        /** 查看按钮操作 */
        handleView(row) {
            this.dialogType = 3
            this.form = row
            this.visible = true
        },
        /** 编辑按钮操作 */
        handleEdit(row) {
            this.dialogType = 2
            this.form = row
            this.visible = true
        },
        /** 关闭抽屉操作 */
        handleClose(row) {
            this.drawer = false
        },
        /** 导出按钮操作 */
        handleExport() {
            let queryParams = {
                ...this.queryParams,
                exportFlag: 1,
            }
            listBomAnalyse(queryParams, 'arraybuffer').then((response) => {
                const aLink = document.createElement('a')
                var blob = new Blob([response])
                aLink.href = URL.createObjectURL(blob)
                console.log(aLink.href)
                aLink.setAttribute(
                    'download',
                    `项目分析_${new Date().getTime()}.xlsx`
                ) // 设置下载文件名称
                document.body.appendChild(aLink)
                aLink.click()
                document.body.removeChild(aLink)
            })
        },
        handleImport() {
            this.fileImportVisible = true
        },
        /** 刷新缓存按钮操作 */
        handleRefreshCache() {
            refreshCache().then(() => {
                this.$modal.msgSuccess(this.$t('tips.refreshSuccess'))
            })
        },
        //工厂模糊查询
        factoryRemoteMethod() {
            listALLFactory({ factoryName: '' }).then((res) => {
                this.factoryOptions = res.data
            })
        },
        handleFactorySelect(e) {
            this.queryParams.factoryName = e.factoryName
        },
        handleFactoryBlur(e) {
            const { factoryOptions, queryParams } = this
            if (!factoryOptions.includes(queryParams.factoryName)) {
                this.queryParams.factoryName = ''
            }
        },
        async handleClick(e) {
            try {
                const formData = new FormData()
                formData.append('file', e.target.files[0])
                const res = await importAnalyse(formData)
                if (res.code == 200) {
                    this.$message.success(
                        this.$t('formErrMsg.importantSuccess')
                    )
                    this.getList()
                    this.$refs['excel-upload-input'].value = ''
                }
            } catch (error) {
                this.$refs['excel-upload-input'].value = ''
            }
        },
        linkBomMasterial(row) {
            this.$router.push({
                path: '/statistics/bommaterial',
                query: {
                    bomAnalyseId: row.id,
                },
            })
        },
    },
}
</script>
<style lang="scss" scoped>
::v-deep .el-card {
    padding-top: 20px;
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 4px;
    padding-bottom: 20px;
}
::v-deep .el-aside {
    padding: 0;
    margin-bottom: 0;
}
.content-style {
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 2px;
    background-color: white;
    margin-top: 10px;
}
::v-deep .el-button-group .selBtn {
    background: #fff;
    border: 1px solid #0b7be3;
    color: #0b7be3;
}
::v-deep .el-button-group .unselBtn {
    background: #fff;
    border: 1px solid #ededf2;
    color: #666666;
}
.header-left {
    font-size: 14px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #333333;
    padding: 3px 0 10px;
    display: inline-flex;
    justify-content: center;
    border-bottom: 2px solid #0b7be3;
}
.header-right {
    float: right;
}
.container-header {
    border-bottom: 1px solid #ededf2;
}
.container-table {
    float: left;
    //margin-top:6px;
    width: 100%;
}
.mb8 {
    float: left;
    margin-top: 6px;
    width: 100%;
}
.drawer-footer {
    display: flex;
    position: relative;
    z-index: 9999;
    bottom: -40px;
    height: 40px;
    justify-content: center;
    width: 100%;
}
.drawer-close {
    background: #f7f8fa;
    border-color: #f7f8fa;
}

.right-btns ::v-deep .el-button {
    margin-top: 0;
}
.app-container {
    padding: 0;
}
.table-action-wrap {
    padding: 12px 0;
}
.el-table {
    width: 99.9% !important; //不让它到临界值，这样可避免自动计算
}
</style>
